package com.yq.bdm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.controller.common.AbstractController;
import com.yq.bdm.entity.GroupInfoEntity;
import com.yq.bdm.service.GroupInfoService;
import com.yq.bdm.utils.PageUtils;
import com.yq.bdm.utils.ResultMap;

/**
 * 组表
 * 
 * @author yuq
 * @date 2017-07-21 16:35:13
 */
@Controller
@RequestMapping("/groupinfo")
public class GroupInfoController extends AbstractController {

	@Autowired
	private GroupInfoService groupInfoService;

	@RequestMapping("/index.html")
	public String list() {
		return "admin/groupinfo/groupinfo.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMap list(String id, String groupId, String parentGroupId,
			String groupName, String description, String gmtCreate,
			String gmtModified) {

		Map<String, Object> map = new HashMap<>();
		// 查询条件
		map.put("id", id);
		map.put("groupId", groupId);
		map.put("parentGroupId", parentGroupId);
		map.put("groupName", groupName);
		map.put("description", description);
		map.put("gmtCreate", gmtCreate);
		map.put("gmtModified", gmtModified);

		// 获取分页参数，添加到查询请求参数中
		getPage(map);

		// 查询列表数据
		List<GroupInfoEntity> groupInfoList = groupInfoService.queryList(map);
		int total = groupInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(groupInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/treeList")
	public ResultMap treeList() {

		// 查询列表数据
		List<GroupInfoEntity> groupInfoList = groupInfoService
				.queryList(null);
		return ResultMap.ok().put("list", groupInfoList);
	}

	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/goAdd")
	public String goAdd() {
		return "admin/groupinfo/add.html";
	}

	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("/goEdit/{id}")
	public String goEdit(@PathVariable("id") Long id) {
		// 设置当前control的访问url，edit页面需要使用
		request.setAttribute("namespaceUrl",
				request.getContextPath() + "/groupinfo");
		GroupInfoEntity groupInfo = groupInfoService.queryObject(id);
		request.setAttribute("groupInfo", groupInfo);
		return "admin/groupinfo/edit.html";
	}

	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{groupId}")
	public ResultMap info(@PathVariable("groupId") Long groupId) {
		GroupInfoEntity groupInfo = groupInfoService.queryObjectByGroupId(groupId);

		return ResultMap.ok().put("groupInfo", groupInfo);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public ResultMap save(@RequestBody GroupInfoEntity groupInfo) {
		groupInfoService.save(groupInfo);

		return ResultMap.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ResultMap update(@RequestBody GroupInfoEntity groupInfo) {
		groupInfoService.update(groupInfo);

		return ResultMap.ok();
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public ResultMap delete(@RequestBody Long[] ids) {
		groupInfoService.deleteBatch(ids);

		return ResultMap.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/deleteByGroupId")
	public ResultMap deleteByGroupId(@RequestBody Long[] groupIds) {
		groupInfoService.deleteBatchByGroupId(groupIds);

		return ResultMap.ok();
	}

	/**
	 * 删除成功后，查询列表的数据
	 */
	@ResponseBody
	@RequestMapping("/deleteAfterList")
	public ResultMap deleteAfterList(@RequestBody Long[] ids) {
		groupInfoService.deleteBatch(ids);

		Map<String, Object> map = new HashMap<>();
		// 获取分页参数，添加到查询请求参数中
		getPage(map);
		// 查询列表数据
		List<GroupInfoEntity> groupInfoList = groupInfoService.queryList(map);
		int total = groupInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(groupInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
}
